<?php

namespace app\models;

use Yii;
use yii\log\Logger;
use yii\validators\EachValidator;
use yii\validators\InlineValidator;
use yii\validators\RequiredValidator;

/**
 * This is the model class for table "attr_name".
 *
 * @property int $id
 * @property string $name 名称
 * @property int $classify_id
 */
class AttrName extends \yii\db\ActiveRecord
{
    public $add_values;
    public $delete_values;

    const SCENARIO_CHANGE = 'change';

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'attr_name';
    }

    public function scenarios()
    {
        return array_merge(parent::scenarios(), [
            self::SCENARIO_CHANGE => [
                'name', 'classify_id', 'add_values', 'delete_values'
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'classify_id'], 'required'],
            [['classify_id'], 'integer'],
            [['name'], 'string', 'max' => 255],
            ['add_values', 'each', 'rule' => [
                function ($attribute, $params, $validator, $current) {
                    /** @var  $validator InlineValidator */
                    $value = new AttrValue();
                    $value->load($current, '');
                    if (!$value->validate()) {
                        $this->addError($attribute, current($value->getFirstErrors()));
                    }
                }], 'on' => self::SCENARIO_CHANGE],
            ['delete_values', 'each', 'rule' => [
                function ($attribute, $params, $validator, $current) {
                    $value = new AttrValue();
                    $value->load($current, '');
                    if (!$value->validate()) {
                        $this->addError($attribute, current($value->getFirstErrors()));
                    }
                }], 'on' => self::SCENARIO_CHANGE]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', 'Name'),
            'classify_id' => Yii::t('app', 'Classify ID'),
        ];
    }

    public function getClassify()
    {
        return $this->hasOne(Classify::class, ['id' => 'classify_id']);
    }

    public function getAttrValues()
    {
        return $this->hasMany(AttrValue::class, ['attr_name_id' => 'id']);
    }

    public function extraFields()
    {
        return array_merge(parent::extraFields(), [
            'classify',
            'attrValues'
        ]); // TODO: Change the autogenerated stub
    }

    public function afterSave($insert, $changedAttributes)
    {
        if ($this->scenario == self::SCENARIO_CHANGE) {
            if ($this->add_values) {
                foreach ($this->add_values as $value) {
                    $child = new AttrValue();
                    $child->load($value, '');
                    $child->attr_name_id = $this->id;
                    $child->save();
                }
            }

            if ($this->delete_values) {
                foreach ($this->delete_values as $value) {
                    $child = AttrValue::findOne($value['id']);
                    $child->delete();
                }
            }

        }
    }

    public function afterDelete()
    {
        parent::afterDelete();
        foreach($this->attrValues as $value){
            $value->delete();
        }
    }
}
